package controllers

import javax.inject.Inject
import play.api.mvc.{AbstractController, ControllerComponents}
import models.Tables._
import dao._
import tool.{FormTool, Tool}
import utils.Utils

import java.io.File
import scala.concurrent.ExecutionContext.Implicits.global


/**
 * Created by yz on 2019/1/16
 */
class TestController @Inject()(cc: ControllerComponents, trashDao: TrashDao, missionDao: MissionDao,
                               pdfInfoDao: PdfInfoDao, extraDataDao: ExtraDataDao, tool: Tool, formTool: FormTool
                              )(implicit val userDao: UserDao,
                                implicit val userNameDao: UserNameDao,
                              ) extends AbstractController(cc) {

  def refreshTrash = Action.async {
    missionDao.selectAll.zip(trashDao.selectAll).flatMap { case (x, trashs) =>
      val rows = x.map(y => TrashRow(y.id, y.userId, true)).filterNot(y => trashs.map(_.id).contains(y.id))
      trashDao.insertAll(rows)
    }.map { x =>
      Ok("success!")
    }
  }

  def deleteUserData = Action.async { implicit request =>
    val data = formTool.idForm.bindFromRequest().get
    userDao.deleteById(data.id).zip(pdfInfoDao.deleteById(data.id)).zip(extraDataDao.deleteByUserId(data.id)).
      zip(missionDao.deleteByUserId(data.id)).zip(trashDao.deleteByUserId(data.id)).
      zip(userNameDao.deleteById(data.id)).map { x =>
      val userDir = new File(Utils.userDir, s"${data.id}")
      Utils.deleteDirectory(userDir)
      Ok("success")
    }
  }

}
